﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common;
using log4net;
using SqlSugar;
using AutoFacUtils;
using CashFlow.Web.Def;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.Db.Repository;
using CashFlow.Web.Service;

namespace CashFlow.Web.Service.Implement
{
    public class ShopUserService : MyService<ShopUserEntity>, IShopUserService
    {
        public IList<UserInfoAndReferenceUserInfo> QueryShopAllUsers(
            int shopManagerUserId
            , int? state)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var query = db.Queryable<ShopUserEntity>()
                    .InnerJoin<UserInfoEntity>((su, u1) => su.UserId == u1.Id)
                    .InnerJoin<UserInfoEntity>((su, u1, u2) => u1.ReferenceInvitationCode == u2.InvitationCode)
                    .Where((su, u1, u2) => su.ShopManagerUserId == shopManagerUserId);

                if (state.HasValue)
                {
                    query = query.Where((su, u1, u2) => u1.State == state.Value);
                }

                var ls = query.Select((su, u1, u2) => new UserInfoAndReferenceUserInfo
                {
                    UserInfo = u1,
                    ReferenceUserInfo = u2
                }).ToList();

                return ls;
            }
        }

        public IList<UserInfoEntity> QueryShopAllUsers2(
            int shopManagerUserId
            , int? state)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var query = db.Queryable<ShopUserEntity>()
                    .InnerJoin<UserInfoEntity>((su, u1) => su.UserId == u1.Id)
                    .Where((su, u1) => su.ShopManagerUserId == shopManagerUserId);

                if (state.HasValue)
                {
                    query = query.Where((su, u1) => u1.State == state.Value);
                }

                var ls = query.Select((su, u1) => u1).ToList();

                return ls;
            }
        }

        public IList<int> QueryShopAllUserIds(int shopManagerUserId, int? state)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var query = db.Queryable<ShopUserEntity>()
                    .InnerJoin<UserInfoEntity>((su, u1) => su.UserId == u1.Id)
                    .Where((su, u1) => su.ShopManagerUserId == shopManagerUserId);

                if (state.HasValue)
                {
                    query = query.Where((su, u1) => u1.State == state.Value);
                }

                var ls = query.Select((su, u1) => u1.Id).ToList();

                return ls;
            }
        }

        public bool IsUserInShop(int userId, int shopManagerUserId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Queryable<ShopUserEntity>()
                          .Where(x => x.ShopManagerUserId == shopManagerUserId
                            && x.UserId == userId)
                          .Count();
                return n > 0;
            }
        }
    }
}
